package com.xiaoyi.utils;

import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils {
    private static DataSource ds = null;
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
    static {
        try {
            Properties prop = new Properties();

            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
            prop.load(in);
            BasicDataSourceFactory factory = new BasicDataSourceFactory();
            ds = factory.createDataSource(prop);
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static DataSource getDataSource() {
        return ds;
    }



    /*
     * 工具类里面现在没有必要提供release()方法，因为我们是使用dbutils操作数据库，
     * 即调用dbutils的update()和query()方法操作数据库，他操作完数据库之后，会自动释放掉连接。
     */
	//释放资源
	public static void relase() throws Exception{

		 DbUtils.close(getConnection());
	}

    /**
     * 从数据源中获取数据库连接
     *
     * @return Connection
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        Connection conn = threadLocal.get();
        if (conn == null) {
            conn = getDataSource().getConnection();
            threadLocal.set(conn);
        }
        return conn;
    }

    /**
     * 开启事务
     *
     */
    public static void startTransaction() {
        try {
            Connection conn = threadLocal.get();
            if (conn == null) {
                conn = getConnection();
                // 把 conn绑定到当前线程上
                threadLocal.set(conn);
            }
            // 开启事务
            conn.setAutoCommit(false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 回滚事务
     *
     */
    public static void rollback() {
        try {
            // 从当前线程中获取Connection
            Connection conn = threadLocal.get();
            if (conn != null) {
                // 回滚事务
                conn.rollback();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 提交事务
     *
     */
    public static void commit() {
        try {
            // 从当前线程中获取Connection
            Connection conn = threadLocal.get();
            if (conn != null) {
                // 提交事务
                conn.commit();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭数据库连接(注意，这里只是把连接还给数据库连接池)
     *
     */
    public static void close() {
        try {
            // 从当前线程中获取Connection
            Connection conn = threadLocal.get();
            if (conn != null) {
                conn.close();
                // 解除当前线程上绑定conn
                threadLocal.remove();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}